#!/usr/bin/python
###############################################################################
#                                                                             #
#    Copyright 2006,2007 Danil Dotsenko                                       #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program; if not, write to the Free Software              #
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA #
#                                                                             #
###############################################################################                                   #
#             Danil Dotsenko, dd@accentsolution.com                           #

#from __future__ import division
import karambino

def debug():
	return False

###########################################################################
# applet settings go here, and are accessible from everywhere as global
class configClass:
	'''
	An all-encoupasing, application-wide configuration storage location.
	This will be shared even between concurrently running themes.
	This is the equivalent of root directory of Windows registry.
	'''
	def __init__(self):
		self.common={
			'name':'Karambino Armatura infrastructure example',
			'version':"v1, March 25, 2007",
			'author':"Danil (Suslik) Dotsenko (dd@accentsolution.com)",
			'cleanStart':False,
			'interval':10000,
			}
		self.lang = None
		self.objects = {}
#
##########################################################

def setupObjects(widget):

	karambino.resizeWidget(widget, 200, 200)

	#button = karambino.imageClass(kdesettings.geticon('down', 32), x=160, y=30, widget=widget)
	text = '12345678901234567890'
	#button = karambino.textClass(text, x=10, y=10, w=120, h=120, widget=widget)
	#button = karambino.fancyInputClass('asdf\nzxcv', x=0, y=0, w=120, h=100, widget=widget)
	parts=['images/t_aa.png',
	'images/t_ab.png',
	'images/t_ac.png',
	'images/t_ba.png',
	'images/t_bc.png',
	'images/t_ca.png',
	'images/t_cb.png',
	'images/t_cc.png']
	#karambino.addFrame(button, parts)
	b = []

	karambino.redrawstatus = False
	for i in [0,1,2,3,4]:
		b.append(karambino.textClass(text, x=0, y=20*i, w=200, h=20, widget=widget))
		b[i].text.size(10+i)
		b[i].text.font('DejaVu Sans Mono')
		b[i].position(0,20*i)
		b[i].text.color(255,255,255)
		b[i].text.align('LEFT')

	img = []
	for i in range(19):
		img.append(karambino.imageClass('blue.png', x=i*10+9,y=0, w=1,h=3, widget=widget))

	karambino.redrawstatus = True

##################################################################
# Testing & helper functions.
#
def notificationWindow(message, title="", mode=None):
	'''
	This uses KDE kdialog app and asks for main body message and window title.
	App name is attached automatically
	'''
	global appConfig
	myCommand = 'kdialog --caption "'+appConfig.common['name']+'" --title "'+title+'" --msgbox "'+message+'"'
	karambino.execute(myCommand)

def using_dcop():
	#import re
	#from commands import getoutput
	a = "(?<=SuperKaramba:) *\d\.\d"
	ret = getoutput('superkaramba --version')
	try:
		skver = float(re.search(a, ret).group().strip())
	except:
		skver = 100
	if skver < 0.5:
		return True
	else:
		return False

def status(widget, text):
	try:
		meter = karambino.getThemeText(widget, "message")
		karambino.changeText(widget, meter, text)
		karambino.redrawWidget(widget)
	except:
		print "Status Report: ", text
#
##################################################################

##################################################################
# KARAMBA's built-in functions
#
#this is called when you widget is initialized
def initWidget(widget):
	global appConfig
	
	status(widget, "Initing the script")
	appConfig=configClass()
	success = True
	
	if success:
		appConfig.common['cleanStart'] = True
		# createMenus(widget)
		setupObjects(widget)

	karambino.redrawWidget(widget)

def widgetUpdated(widget):
	global appConfig
	if appConfig.common['cleanStart']:
		karambino.changeInterval(widget, appConfig.common['interval'])

def widgetClosed(widget):
	global appConfig
	try:
		if appConfig.common['cleanStart']:
			# put working stuff here
			pass
	except:
		pass

def itemDropped(widget, dropText, x=0, y=0):
	print "in the itemDrag dropText is ", dropText, type(dropText)

#  botton = button clicked:
#                    1 = Left Mouse Button
#                    2 = Middle Mouse Button
#                    3 = Right Mouse Button
#                    4,5 = Scroll wheel up and down
def widgetClicked(widget, x, y, button):
	#print "widgetClicked function, button is ", button
	pass

# NOTE you must use attachClickArea() to make a meterClicked work
def meterClicked(widget, meter, button):
	karambino.meterClicked(widget, meter, button)

def keyPressed(widget, meter, char):
	karambino.keyPressed(widget, meter, char)

def themeNotify(widget, caller, message):
	pass
	
def widgetMouseMoved(widget, x, y, button):
	#Warning:  Don't do anything too intensive here
	karambino.widgetMouseMoved(widget, x, y, button)

# This will be printed when the widget loads.
print "Karambino example is loaded."
